﻿<?php
	class Member{
		public $id;
		public $password;
		public $nickname;
		public $email;
		public $userName;
		
		public function __construct(){
			
		}
		
		public function display($id, $password){
			echo "<html> \n <head>\n</head>\n<body>\n";
			if (($id!="") && ($password != "")){
				$this->id = $id;
				$this->password = $password;
			} else{
				echo "아이디와 비밀번호를 입력하시오.";
				exit;
			}
			$this->compare();
			$this->page($id);
			echo "</body>\n</html> \n";
		}
		
		public function createMember($id, $password, $username, $email, $nickname){
			if (($id!="") && ($password != "") && ($username != "") && ($email != "") && ($nickname != "")){
				$this->id = $id;
				$this->password = $password;
				$this->username = $username;
				$this->email = $email;
				$this->nickname = $nickname;
				
				@ $db = new mysqli(_DB_HOST_ , _DB_USER_ , _DB_PASSWORD_, _DB_DATABASE_ );
				if ($db->connect_errno) {
					echo "connection error!!";
					exit;
				}
				//encoding setup
				$db->set_charset("utf8");
				$query = "select * from members where id = '" . $this->id . "'";
				$result = $db->query($query);
				if ($result->num_rows ==0){
					$query = "insert into members(id, password, username, email, nickname) values ('" . $this->id . "', SHA1('" . $this->password . "'), '" . $this->username . "', '" . $this->email . "', '" . $this->nickname . "')" ;
					
					$db->query($query);
					echo "회원가입이 완료되었습니다.";
				}else{
					echo "아이디가 중복됩니다. 다시 입력해주세요";
					exit;
				}
			}else{
				echo "빈칸을 모두 채워주세요.";
				exit;
			}
		
		}
		
		protected function compare(){
			//DB에서 필요한 정보를 읽어오기
			@ $db = new mysqli(_DB_HOST_ , _DB_USER_ , _DB_PASSWORD_, _DB_DATABASE_ );
			if ($db->connect_errno) {
				echo "connection error!!";
				exit;
			}
			
			//encoding setup
			$db->set_charset("utf8");
			
			$query = "select * from members where id = '" . $this->id . "'";
			$result = $db->query($query);
			if($result->num_rows == 0 ){
				echo "입력하신 아이디가 없습니다.";
				exit;
			}
			else{
				$row = $result->fetch_assoc();
				if($this->password == $row['password']){
					$this->nickname = $row['nickname'];
					$this->userName = $row['username'];
					$this->email = $row['email'];
					return;
				}else {
					echo "아이디와 비밀번호가 일치하지 않습니다.";
					exit;
				}
			}
		}
		
		protected function master(){
		
			echo "<table border='1'>\n <tr>\n <td>id</td>\n <td>password</td>\n <td>username</td>\n <td>nickname</td>\n <td>email</td>\n </tr>\n";
			//DB에서 필요한 정보를 읽어오기
			@ $db = new mysqli(_DB_HOST_ , _DB_USER_ , _DB_PASSWORD_, _DB_DATABASE_ );
			if ($db->connect_errno) {
				echo "connection error!!";
				exit;
			}
			
			//encoding setup
			$db->set_charset("utf8");
			
			$query = "select * from members";
			$result = $db->query($query);
			echo "<h1>회원 정보 보기</h1>";
			echo "<p>*회원의 자세한 정보를 보려면 아이디를 누르세요.</p>";
			for($i=0;$i<$result->num_rows;$i++){
				$row = $result->fetch_assoc();
				echo "<tr>\n";
				echo "<td><a href='login.php?mode=view&key=" . $row['id'] . "'>" . $row['id'] . "</a></td>\n" . "<td>" . $row['password'] . "</td>\n" . "<td>" . $row['username'] . "</td>\n" . "<td>" . $row['nickname'] . "</td>\n" . "<td>" . $row['email'] . "</td>\n";
				echo "</tr>\n";
			}
			echo "</table>\n";
		}
		
		public function view($id) {
			//DB에서 필요한 정보를 읽어오기
			@ $db = new mysqli(_DB_HOST_ , _DB_USER_ , _DB_PASSWORD_, _DB_DATABASE_ );

			if ($db->connect_errno) {
				echo "connection error!!";
				exit;
			}
			
			//encoding setup
			$db->set_charset("utf8");
			
			$query = "select * from members where id = '" . $id . "'";
			$result = $db->query($query);
			
			$row = $result->fetch_assoc();	
			echo "<h1>회원정보 상세 보기</h1>";
			
			
			if (($row['id'] != "") &&($row['password'] != "") &&($row['nickname'] != "") &&($row['username'] != "") &&($row['email'] != "")){
				echo "<table border='1'>\n";
				echo "<tr>\n";
				echo "<td>id</td>\n";
				echo "<td>" . $row['id'] . "</td>\n";
				echo "</tr>\n";
				echo "<tr>\n";
				echo "<td>password</td>\n";
				echo "<td>" . $row['password'] . "</td>\n";
				echo "</tr>\n";
				echo "<tr>\n";
				echo "<td>username</td>\n";
				echo "<td>" . $row['username'] . "</td>\n";
				echo "</tr>\n";
				echo "<tr>\n";
				echo "<td>nickname</td>\n";
				echo "<td>" . $row['nickname'] . "</td>\n";
				echo "</tr>\n";
				echo "<tr>\n";
				echo "<td>email</td>\n";
				echo "<td>" . $row['email'] . "</td>\n";
				echo "</tr>\n";
				echo "</table>\n" . "<br/>";
				
				
				echo "<form name='input' action='login.php?mode=edit&key=" . $id . "' method='post'>\n";
				echo "<input type='submit' value='수정'/>\n";
				echo "</form>";
				
				echo "<form name='input' action='login.php?mode=delete&key=" . $id . "' method='post'>\n";
				echo "<input type='submit' value='삭제'/>\n";
				echo "</form>";
			} else {
				echo "<p>회원 정보가 없습니다.</p>";
			}
		}
		
		public function editForm($id){
			//DB에서 필요한 정보를 읽어오기
			@ $db = new mysqli(_DB_HOST_ , _DB_USER_ , _DB_PASSWORD_, _DB_DATABASE_ );

			if ($db->connect_errno) {
				echo "connection error!!";
				exit;
			}
			
			//encoding setup
			$db->set_charset("utf8");
			
			$query = "select * from members where id = '" . $id . "'";
			$result = $db->query($query);
			
			$row = $result->fetch_assoc();
			
			echo "<h1>회원정보 수정</h1>";
	
			echo "<form name='input' action='login.php?mode=editView&key=" . $id . "' method='post'>";
			echo "이름 : <input type = 'text' name='username' value='" . $row['username'] . "' /><br />";
			echo "이메일 : <input type = 'text' name = 'email' value='" . $row['email'] . "' /><br />";
			echo "비밀번호 : <input type='password' name='password' /><br />";
			echo "닉네임 : <input type='text' name='nickname' value='" . $row['nickname'] . "' /><br />";
			echo "<input type='submit' value='확인' />
					</form>";
		}
		
		public function edit($key, $password, $username, $nickname, $email){
			//DB에서 필요한 정보를 읽어오기
			@ $db = new mysqli(_DB_HOST_ , _DB_USER_ , _DB_PASSWORD_, _DB_DATABASE_ );

			if ($db->connect_errno) {
				echo "connection error!!";
				exit;
			}
			
			//encoding setup
			$db->set_charset("utf8");
			$query = "update members set id = '" . $key . "'";
			if ($password != "") {
				$query .= ", password = '" . $password . "'";
			} if ($username != "") {
				$query .= ", username = '" . $username . "'";
			} if ($email != "") {
				$query .= ", email = '" . $email . "'";
			} if ($nickname != "") {
				$query .= ", nickname = '" . $nickname . "'";
			}
			$query .= "where id = '" . $key . "'";
			$db->query($query);
			echo "변경이 완료 되었습니다.";
			
			echo "<form name='input' action='login.php?mode=view&key=" . $key . "' method='post'>\n
					<input type = 'submit' value = '보러가기' />\n
					</form>";
		}
		
		public function delete($key){
			//DB에서 필요한 정보를 읽어오기
			@ $db = new mysqli(_DB_HOST_ , _DB_USER_ , _DB_PASSWORD_, _DB_DATABASE_ );

			if ($db->connect_errno) {
				echo "connection error!!";
				exit;
			}
			
			//encoding setup
			$db->set_charset("utf8");
			$query = "delete from members where id = '" . $key . "'";
			$db->query($query);
			echo "삭제가 완료 되었습니다.";
			
			echo "<form name='input' action='login.php?mode=view&key=" . $key . "' method='post'>\n
					<input type = 'submit' value = '보러가기' />\n
					</form>";
		}
		
		protected function page($id) {
			session_start();
			$_SESSION['user']=$id;
			
			if($_SESSION['user']==master) {
				$this->master();
			} elseif (isset($_SESSION['user'])) {
				echo $this->userName . "[" . $this->nickname . "] 님 환영합니다. 당신의 이메일은 " . $this->email . "입니다.";
			} else {
				echo "login please";
			}
		}
	}
?>
